<!-- 转台：转移桌台的组件 -->
<template>
  <CustomDialog
    v-model:visible="dialogVisible"
    top="5vh"
    width="720px"
    curent-class="transfer-desktop-dialog"
  >
    <div class="dialog-box">
      <div class="title">转台</div>
      <div class="paging-list-box">
        <div
          class="paging-list-item"
          :class="{ 'paging-list-item-active': pagingActiveIndex === pagingIndex }"
          @click="handleSelectPaging(pagingIndex, pagingItem)"
          v-for="(pagingItem, pagingIndex) in desktopCategoryList"
          :key="pagingItem.id + pagingIndex"
        >
          <el-tooltip class="item" effect="dark" :content="pagingItem.name" placement="top">
            <div class="name">{{ pagingItem.name }}</div>
          </el-tooltip>
        </div>
        <div class="paging-list-btn">
          <img
            class="left-btn"
            @click.stop="oneCatePreviousPage"
            src="@/assets/images/home/page-left-icon.png"
            alt=""
          />
          <img
            @click.stop="oneCateNextPage"
            class="right-btn"
            src="@/assets/images/home/page-right-icon.png"
            alt=""
          />
        </div>
      </div>
      <div class="desktop-form-box">
        <!-- 已选中的桌台 -->
        <div class="room-selected-box" v-loading="listLoading">
          <div class="room-selected-scroll-list" v-if="!isEmptyDesktopList">
            <div
              class="scroll-selected-list-item"
              v-for="(desk, index) in desktopList"
              :key="desk.id + index"
              @click="handleSelectTable(index, desk)"
            >
              <div class="title-box">
                <div class="count">
                  <div class="hall">{{ desk.region?.name }}</div>
                  <div class="num">{{ desk.capacity }}人桌</div>
                </div>
              </div>
              <div class="desc-box">
                <div class="numberAndTag">
                  <div class="number">{{ desk.name }}</div>
                </div>
              </div>
              <el-icon v-show="selectedTableIndex === index" class="choose-img"><Select /></el-icon>
            </div>
          </div>
          <!-- 暂无数据的组件 -->
          <div class="no-data-box" v-else>
            <el-empty description="暂无桌台数据" />
          </div>
        </div>
        <div class="count-selected-box" v-if="!isEmptyDesktopList">
          <div class="total">共{{ desktopListTotal }}台</div>
        </div>
      </div>
      <!-- 选择进行转移的操作 -->
      <div class="transform-box">
        <div class="title">选择转台桌台</div>
        <div class="transform-content">
          <div class="left-box">
            <div class="title-box">
              <div class="count">
                <div class="hall" v-if="mainSelectCurrentDeskData?.region">
                  {{ mainSelectCurrentDeskData.region.name }}
                </div>
                <div class="num">
                  <span v-if="mainSelectCurrentDeskData?.recordInfo">
                    {{ mainSelectCurrentDeskData.recordInfo.use_num }}
                  </span>
                  /
                  <span v-if="mainSelectCurrentDeskData?.capacity">
                    {{ mainSelectCurrentDeskData.capacity }}
                  </span>
                  人
                </div>
              </div>
            </div>
            <div class="desc-box">
              <div class="numberAndTag">
                <div class="number">{{ mainSelectCurrentDeskData?.name }}</div>
              </div>
            </div>
          </div>
          <div class="arrow-box">
            <div class="image">
              <el-icon style="font-size: 24px; color: #7c0a03" class="right-icon"><Right /></el-icon>
            </div>
            <div class="desc">转台</div>
          </div>
          <div class="right-box-empty" v-if="selectedTableIndex === -1">请在上方选择转台桌台</div>
          <div class="right-box-selected" v-else>
            <div class="title-box">
              <div class="count">
                <div class="hall">{{ selectDeskActiveData?.region?.name }}</div>
                <div class="num">{{ selectDeskActiveData?.capacity }}人桌</div>
              </div>
            </div>
            <div class="desc-box">
              <div class="numberAndTag">
                <div class="number">{{ selectDeskActiveData?.name }}</div>
              </div>
            </div>
          </div>
        </div>
        <div class="button-box">
          <div class="left-desc"></div>
          <div class="btn-item" v-if="selectedTableIndex === -1">确定</div>
          <!-- 选中转台 按钮 -->
          <div
            class="btn-item-active"
            v-else
            @click="onConfirm"
            :class="{ 'loading-btn': loading }"
          >
            <el-icon class="loading-icon" v-if="loading"><Loading /></el-icon>
            <span>确定</span>
          </div>
        </div>
      </div>
    </div>
  </CustomDialog>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { Right, Select, Loading } from '@element-plus/icons-vue'
import { isEmpty } from '@/utils/toolsValidate'
import { fetchDeskList, fetchCategoryList, moveDesk } from '@/api/desktop'
import CustomDialog from '@/components/CustomDialog/Index.vue'

interface Props {
  modelValue: boolean
  deskDetail?: any
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'success'): void
}

const props = withDefaults(defineProps<Props>(), {
  modelValue: false,
  deskDetail: null
})

const emit = defineEmits<Emits>()

// 对话框显示状态
const dialogVisible = computed({
  get: () => props.modelValue,
  set: val => emit('update:modelValue', val)
})

// 需要进行转移操作的原来的桌台数据
const mainSelectCurrentDeskData = computed(() => props.deskDetail)

// 桌台分类的分页专用索引
const pagingActiveIndex = ref(0)

// 桌台列表相关查询数据
const desktopQuery = ref({
  // 桌台状态，只筛选空闲中的桌台
  is_use: 0,
  // 分类id
  category_id: null as number | null
})

// 选中对应桌台的索引下标
const selectedTableIndex = ref(-1)

// 桌台列表数据
const desktopList = ref<any[]>([])

// 桌台列表数据总条数
const desktopListTotal = ref(0)

// 列表为空是否暂无数据
const isEmptyDesktopList = ref(false)

// 桌台分类列表
const desktopCategoryList = ref<any[]>([])

// 已经选中的桌台数据
const selectDeskActiveData = ref<any>(null)

// 桌台分类列表分页相关
const deskCategoryPageQuery = ref({
  // 一级分类的分页
  page: 1,
  limit: 4
})

// 判断一级分类是否还能继续下一页操作分页
const deskOneCategoryLastPage = ref(0)

// 加载状态
const loading = ref(false)

// 监听对话框显示状态
watch(
  () => dialogVisible.value,
  val => {
    if (val) {
      // 重置数据
      resetData()
      // 获取桌台列表数据，只筛选空闲中的桌台
      getDeskListData()
      // 获取桌台分类数据
      getCategoryListData()
    }
  }
)

// 桌台列表加载状态
const listLoading = ref(false)

// 获取桌台列表数据
const getDeskListData = async () => {
  listLoading.value = true
  try {
    const res = await fetchDeskList(desktopQuery.value)
    if (res.status === 200 && !isEmpty(res?.data)) {
      desktopList.value = res.data
      desktopListTotal.value = res.data.length
      isEmptyDesktopList.value = res.data.length === 0
    } else {
      desktopList.value = []
      desktopListTotal.value = 0
      isEmptyDesktopList.value = true
    }
  } catch (error) {
    console.error('获取桌台列表失败:', error)
    desktopList.value = []
    desktopListTotal.value = 0
    isEmptyDesktopList.value = true
  } finally {
    listLoading.value = false
  }
}

// 分类列表加载状态
const categoryLoading = ref(false)

// 获取桌台分类列表数据
const getCategoryListData = async () => {
  categoryLoading.value = true
  if (deskCategoryPageQuery.value.page === 1) {
    deskCategoryPageQuery.value.limit = 4
  } else {
    deskCategoryPageQuery.value.limit = 5
  }

  try {
    const res = await fetchCategoryList(deskCategoryPageQuery.value)
    if (res.status === 200 && !isEmpty(res?.data?.data)) {
      deskOneCategoryLastPage.value = res.data.last_page
      if (deskCategoryPageQuery.value.page === 1) {
        res.data.data.unshift({
          id: 0,
          name: '全部'
        })
      }
      desktopCategoryList.value = res.data.data
    }
  } catch (error) {
    console.error('获取桌台分类列表失败:', error)
  } finally {
    categoryLoading.value = false
  }
}

// 操作分页筛选对应的分类
const handleSelectPaging = (index: number, item: any) => {
  pagingActiveIndex.value = index
  desktopQuery.value.category_id = item.id || null
  getDeskListData()
}

// 确认提交事件
const onConfirm = async () => {
  if (selectedTableIndex.value === -1) {
    ElMessage.warning('请先选择桌台再操作')
    return
  }

  if (loading.value) return // 防止重复点击

  loading.value = true

  try {
    const datas = {
      record_id: mainSelectCurrentDeskData.value.recordInfo.id, // 源桌台ID
      target_desk_id: selectDeskActiveData.value.id // 目标桌台ID
    }

    const res = await moveDesk(datas)
    if (res.status === 200) {
      ElMessage.success(`转台成功至${selectDeskActiveData.value.name}桌台`)
      // 通知父组件刷新数据
      emit('success')
      // 关闭对话框
      dialogVisible.value = false
    } else {
      ElMessage.error('转台失败')
    }
  } catch (error) {
    console.error('转台操作失败:', error)
    ElMessage.error('转台失败')
  } finally {
    loading.value = false
  }
}

// 选中对应的桌台
const handleSelectTable = (index: number, data: any) => {
  selectedTableIndex.value = index
  selectDeskActiveData.value = data
}

// 一级分类上一页操作
const oneCatePreviousPage = () => {
  if (deskCategoryPageQuery.value.page === 1) return
  deskCategoryPageQuery.value.page -= 1
  // 重置一级分类索引和桌台列表分页
  resetOneCate()
  getCategoryListData()
}

// 一级分类下一页操作
const oneCateNextPage = () => {
  if (deskCategoryPageQuery.value.page >= deskOneCategoryLastPage.value) return
  deskCategoryPageQuery.value.page += 1
  // 重置一级分类索引和桌台列表分页
  resetOneCate()
  getCategoryListData()
}

// 重置一级分类索引和桌台列表分页
const resetOneCate = () => {
  // 重置索引
  pagingActiveIndex.value = 0
  selectedTableIndex.value = -1
  // 重置分类id
  desktopQuery.value.category_id = null
  // 获取桌台列表数据
  getDeskListData()
}

// 重置数据
const resetData = () => {
  // 桌台分类的分页专用索引
  pagingActiveIndex.value = 0
  // 桌台列表相关查询数据
  desktopQuery.value = {
    is_use: 0,
    category_id: null
  }
  // 选中对应桌台的索引下标
  selectedTableIndex.value = -1
  // 桌台列表数据
  desktopList.value = []
  // 桌台列表数据总条数
  desktopListTotal.value = 0
  // 列表为空是否暂无数据
  isEmptyDesktopList.value = false
  // 桌台分类列表
  desktopCategoryList.value = []
  // 已经选中的桌台数据
  selectDeskActiveData.value = null
  // 桌台分类列表分页相关
  deskCategoryPageQuery.value = {
    // 一级分类的分页
    page: 1,
    limit: 4
  }
  // 判断一级分类是否还能继续下一页操作分页
  deskOneCategoryLastPage.value = 0
}
</script>

<style lang="scss" scoped>
.dialog-box {
  width: 720px;
  box-sizing: border-box;
}

.title {
  padding-left: 20px;
  font-size: 20px;
  font-family:
    PingFang SC,
    PingFang SC-Bold;
  font-weight: 700;
  text-align: left;
  color: #333333;
  margin-bottom: 30px;
}

.paging-list-box {
  position: relative;
  box-sizing: border-box;
  margin: 0 10px;
  width: 700px;
  height: 48px;
  background: #ffffff;
  border: 1px solid #d0d0d0;
  border-radius: 25px;
  display: flex;
  align-items: center;
  margin-bottom: 30px;

  .paging-list-item {
    display: inline-block;
    padding: 0 30px;
    height: 48px;
    line-height: 48px;
    font-size: 16px;
    font-family:
      PingFang SC,
      PingFang SC-Regular;
    font-weight: 400;
    text-align: center;
    color: #333333;
    cursor: pointer;

    .name {
      max-width: 60px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }

  .paging-list-item-active {
    color: #ffffff;
    background: #7c0a03;
    border-radius: 24px;
  }

  .paging-list-btn {
    position: absolute;
    top: 12px;
    right: 16px;
    display: flex;
    align-items: center;

    .left-btn,
    .right-btn {
      width: 24px;
      height: 24px;
      cursor: pointer;
    }

    .left-btn {
      margin-right: 20px;
    }
  }
}

.room-selected-box {
  padding: 0 20px 20px 20px;
  box-sizing: border-box;
  height: 252px;
  overflow-y: auto;
  background: #f4f4f4;

  .room-selected-scroll-list {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    justify-content: space-between;

    .scroll-selected-list-item {
      position: relative;
      width: 208px;
      height: 203px;
      background: #ffffff;
      border: 1px solid #d2d2d2;
      border-radius: 12px;
      box-sizing: border-box;
      border-top: none;
      margin-top: 20px;
      cursor: pointer;

      .title-box {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        height: 43px;
        background: #d2d2d2;
        padding: 0 12px;
        font-size: 13px;
        font-family:
          PingFang SC,
          PingFang SC-Bold;
        font-weight: 700;
        color: #ffffff;
        border-radius: 12px 12px 0px 0px;

        .count {
          display: flex;
          align-items: center;

          .hall {
            margin-right: 8px;
          }
        }
      }

      .desc-box {
        padding: 10px 0 0 12px;

        .numberAndTag {
          display: flex;
          align-items: flex-start;

          .number {
            font-size: 34px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #434447;
          }
        }

        .price {
          margin-top: 30px;
          font-size: 16px;
          font-family:
            PingFang SC,
            PingFang SC-Regular;
          font-weight: 400;
          color: #999999;
        }
      }
    }

    .choose-img {
      position: absolute;
      right: 12px;
      bottom: 12px;
      width: 24px;
      height: 24px;
      color: #7c0a03;
    }
  }

  .room-selected-scroll-list::after {
    content: '';
    width: 208px;
  }
}

//滚动条整体的大小
.room-selected-box::-webkit-scrollbar {
  width: 6px !important;
  height: 0 !important;
}

//滚动条里滑动的滑块
.room-selected-box::-webkit-scrollbar-thumb {
  height: 100px !important;
  background: #d2d2d2 !important;
  border-radius: 3px !important;
}

//滚动条底层样式
.room-selected-box::-webkit-scrollbar-track {
  display: none !important;
}

.count-selected-box {
  height: 60px;
  padding-left: 20px;
  box-sizing: border-box;
  background: #f4f4f4;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  font-size: 14px;
  font-family:
    PingFang SC,
    PingFang SC-Regular;
  font-weight: 400;
  color: #333333;
}

.transform-box {
  margin-top: 30px;

  .title {
    font-size: 16px;
    font-family:
      PingFang SC,
      PingFang SC-Regular;
    font-weight: 400;
    text-align: left;
    color: #333333;
    padding-left: 10px;
    margin-bottom: 20px;
  }

  .transform-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 40px;

    .left-box {
      width: 208px;
      height: 203px;
      background: #ffffff;
      border: 1px solid #c97f7a;
      border-radius: 12px;
      box-sizing: border-box;
      border-top: none;
      margin-top: 20px;

      .title-box {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        height: 43px;
        background: #c97f7a;
        padding: 0 12px;
        font-size: 13px;
        font-family:
          PingFang SC,
          PingFang SC-Bold;
        font-weight: 700;
        color: #ffffff;
        border-radius: 12px 12px 0px 0px;

        .count {
          display: flex;
          align-items: center;

          .hall {
            margin-right: 8px;
          }
        }
      }

      .desc-box {
        padding: 10px 0 0 12px;

        .numberAndTag {
          display: flex;
          align-items: flex-start;

          .number {
            font-size: 34px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #434447;
          }
        }

        .price {
          margin-top: 30px;
          font-size: 16px;
          font-family:
            PingFang SC,
            PingFang SC-Regular;
          font-weight: 400;
          color: #999999;
          line-height: 20px;
        }
      }
    }

    .arrow-box {
      position: relative;
      width: 224px;
      height: 10px;
      display: flex;
      align-items: center;
      justify-content: center;

      .image {
        width: 100%;
        height: 2px;
        background-color: #7c0a03;
        position: relative;
      }

      .desc {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        z-index: 2;
        width: 90px;
        height: 30px;
        line-height: 30px;
        background: #ffffff;
        border: 1px solid #7c0a03;
        border-radius: 6px;
        font-size: 14px;
        font-family:
          PingFang SC,
          PingFang SC-Regular;
        font-weight: 400;
        text-align: center;
        color: #7c0a03;
      }
    }

    .right-box-empty {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 208px;
      height: 203px;
      background: #ffffff;
      border: 1px solid #d2d2d2;
      border-radius: 12px;
      font-size: 14px;
      font-family:
        PingFang SC,
        PingFang SC-Regular;
      font-weight: 400;
      color: #000000;
    }

    .right-box-selected {
      width: 208px;
      height: 203px;
      background: #ffffff;
      border: 1px solid #d2d2d2;
      border-radius: 12px;
      box-sizing: border-box;
      border-top: none;
      margin-top: 20px;
      cursor: pointer;

      .title-box {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        height: 43px;
        background: #d2d2d2;
        padding: 0 12px;
        font-size: 13px;
        font-family:
          PingFang SC,
          PingFang SC-Bold;
        font-weight: 700;
        color: #ffffff;
        border-radius: 12px 12px 0px 0px;

        .count {
          display: flex;
          align-items: center;

          .hall {
            margin-right: 8px;
          }
        }
      }

      .desc-box {
        padding: 10px 0 0 12px;

        .numberAndTag {
          display: flex;
          align-items: flex-start;

          .number {
            font-size: 34px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #434447;
          }
        }

        .price {
          margin-top: 30px;
          font-size: 16px;
          font-family:
            PingFang SC,
            PingFang SC-Regular;
          font-weight: 400;
          color: #999999;
          line-height: 20px;
        }
      }
    }
  }

  .button-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 52px;
    margin-top: 50px;

    .left-desc {
      display: flex;
      align-items: center;

      .image {
        flex-shrink: 0;
        width: 24px;
        height: 24px;
        margin-right: 10px;
      }

      .desc {
        font-size: 16px;
        font-family:
          PingFang SC,
          PingFang SC-Regular;
        font-weight: 400;
        text-align: left;
        color: #333333;
      }
    }

    .btn-item {
      width: 120px;
      height: 52px;
      line-height: 52px;
      background: #d2d2d2;
      border-radius: 12px;
      font-size: 18px;
      font-family:
        PingFang SC,
        PingFang SC-Regular;
      font-weight: 400;
      text-align: center;
      color: #ffffff;
      cursor: pointer;
    }

    .btn-item-active {
      width: 120px;
      height: 52px;
      line-height: 52px;
      background: #7c0a03;
      border-radius: 12px;
      font-size: 18px;
      font-family:
        PingFang SC,
        PingFang SC-Regular;
      font-weight: 400;
      text-align: center;
      color: #ffffff;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;

      &.loading-btn {
        cursor: not-allowed;
        opacity: 0.8;
      }

      .loading-icon {
        margin-right: 5px;
        animation: rotate 1s linear infinite;
      }

      @keyframes rotate {
        from {
          transform: rotate(0deg);
        }
        to {
          transform: rotate(360deg);
        }
      }
    }
  }
}

.no-data-box {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 252px;
}
.right-icon {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  right: -6px;
}
</style>
<style>
.transfer-desktop-dialog {
  max-height: unset !important;
}
</style>
